#include <iostream>
#include <vector>
#include <Vector.hpp>
#include <algorithm>

using namespace std;

class Solution
{
public:
    vector<vector<int>> merge(vector<vector<int>> &intervals)
    {
        vector<vector<int>> ans;
        sort(intervals.begin(), intervals.end());
        for (const auto &i : intervals)
        {
            if (ans.empty() || ans.back()[1] < i[0])
            {
                ans.push_back(i);
            }
            else if (ans.back()[1] < i[1])
            {
                ans.back()[1] = i[1];
            }
        }
        return ans;
    }
};

int main()
{
    cout << "Code56" << endl;
    Solution s;
    vector<vector<int>> intervals = {{1, 3}, {1, 2}, {1, 5}, {2, 6}, {8, 10}, {15, 18}};
    vector<vector<int>> ans = s.merge(intervals);
    Print2DVector(ans);
    return 0;
}